package com.rdzn.mall.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rdzn.mall.base.common.Result;
import com.rdzn.mall.center.entity.PmsKeywords;
import com.rdzn.mall.center.entity.UmsMember;
import com.rdzn.mall.center.service.IPmsKeywordsService;
import com.rdzn.mall.center.service.IUmsSearchHistoryService;
import com.rdzn.mall.portal.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
* @Description: search
* @Author: jeecg-boot
* @Date:   2020-09-15
* @Version: V1.0
*/
@Api(tags="search")
@RestController
@RequestMapping("/search")
@Slf4j
public class MSearchController extends JeecgController<PmsKeywords, IPmsKeywordsService> {
   @Autowired
   private IPmsKeywordsService keywordsService;
   @Autowired
   private IUmsSearchHistoryService searchHistoryService;
   @Autowired
   private IUserService userService;

    /**
     *
     * @return
     */
    @AutoLog(value = "search-分页列表查询")
    @ApiOperation(value="search-分页列表查询", notes="search-分页列表查询")
    @GetMapping(value = "/index")
    public Result<?> index() {
        Map<String, Object> data = new HashMap<>();
        data.put("defaultKeyword", keywordsService.listDefault(1));
        data.put("hotKeywordList", keywordsService.listHot(10));
        UmsMember umsMember = userService.getCurrentMember();
        if (umsMember != null){
            data.put("historyKeywordList", searchHistoryService.listByUserId(userService.getCurrentMember().getId(), 10));
        }
        return Result.ok(data);
    }

    /**
     *
     * @param pmsKeywords
     * @param req
     * @return
     */
    @AutoLog(value = "search-分页列表查询")
    @ApiOperation(value="search-分页列表查询", notes="search-分页列表查询")
    @GetMapping(value = "/helper")
    public Result<?> helper(PmsKeywords pmsKeywords,
                           HttpServletRequest req) {
        return Result.ok(keywordsService.list(pmsKeywords.getKeyword(), 10));
    }

   /**
    * 分页列表查询
    *
    * @param pmsKeywords
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "search-分页列表查询")
   @ApiOperation(value="search-分页列表查询", notes="search-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(PmsKeywords pmsKeywords,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<PmsKeywords> queryWrapper = QueryGenerator.initQueryWrapper(pmsKeywords, req.getParameterMap());
       Page<PmsKeywords> page = new Page<PmsKeywords>(pageNo, pageSize);
       IPage<PmsKeywords> pageList = keywordsService.page(page, queryWrapper);
       return Result.ok(pageList);
   }



   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "search-通过id删除")
   @ApiOperation(value="search-通过id删除", notes="search-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       keywordsService.removeById(id);
       return Result.ok("删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "search-通过id查询")
   @ApiOperation(value="search-通过id查询", notes="search-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       PmsKeywords pmsKeywords = keywordsService.getById(id);
       if(pmsKeywords==null) {
           return Result.error("未找到对应数据");
       }
       return Result.ok(pmsKeywords);
   }

}
